import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:sweetim/model/friends.dart';
import 'package:sweetim/model/member_info.dart';
import 'package:sweetim/service/friends_service.dart';
import 'package:sweetim/service/login_service.dart';
import 'package:sweetim/service/member_service.dart';

class FriendsController extends GetxController {
  final FriendsService _friendsService = Get.put(FriendsService());

  var loading = false.obs;

  final PagingController<int, Friends> pagingController =
  PagingController(firstPageKey: 0);
  int _pageSize = 50;

  @override
  onInit() async {
    Get.log("task controller oninit");
    pagingController.addPageRequestListener((pageKey) {
      _fetchFriendsList(pageKey);
    });
    super.onInit();
  }

  _fetchFriendsList(int pageKey) async {
    Get.log("pageKey: ${pageKey}");
    List<Friends>? list = await _friendsService.queryFriends(start: pageKey);
    if (list != null) {
      final isLastPage = list.length < _pageSize;
      if (isLastPage) {
        pagingController.appendLastPage(list);
      } else {
        final nextPageKey = pageKey + list.length;
        pagingController.appendPage(list, nextPageKey);
      }
    }
  }

  @override
  onClose() {
    pagingController.dispose();
    super.onClose();
  }


}